﻿using MediatR;
using SqlSugar;
using WarehouseManagement.Application.Command.RequestTransferCommand;
using WarehouseManagement.Domain.DTO.RequestTransfer;
using WarehouseManagement.Domain.DTO.ShipApply;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.RequestTransferCommandHandlers
{
    /// <summary>
    /// 显示调拨申请列表的命令处理器
    /// </summary>
    public class RequestTransferSelectCommandHandlers : IRequestHandler<RequestTransferSelectCommand, PageDTO<SelectRequestTransfersDto>>
    {
        private readonly IRequestTransferRepository _requestTransferRep;
        private readonly IAllocationProductAppRepository _allocationProductAppRep;
        protected readonly ISqlSugarClient db;

        public RequestTransferSelectCommandHandlers(IRequestTransferRepository requestTransferRep, IAllocationProductAppRepository allocationProductAppRep, ISqlSugarClient db)
        {
            this._requestTransferRep = requestTransferRep;
            this._allocationProductAppRep = allocationProductAppRep;
            this.db = db;
        }

        public async Task<PageDTO<SelectRequestTransfersDto>> Handle(RequestTransferSelectCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<RequestTransfers, AllocationProductApp>((requesttransfers, allocationproductapp) => new JoinQueryInfos(
                    JoinType.Inner, requesttransfers.RTNumber == allocationproductapp.RTNumber))
                .Where((requesttransfers, allocationproductapp) => requesttransfers.IsDel == false)
                .OrderBy((requesttransfers, allocationproductapp) => new
                {
                    requesttransfers.RequestTransfersId,
                    requesttransfers.RTName,
                    requesttransfers.RTNumber,
                    requesttransfers.RTAuditors,
                    requesttransfers.RTSmark,
                })
                .Select((requesttransfers, allocationproductapp) => new SelectRequestTransfersDto
                {
                    RequestTransfersId = requesttransfers.RequestTransfersId,
                    RTName = requesttransfers.RTName,
                    RTNumber = requesttransfers.RTNumber,
                    RTAuditors = requesttransfers.RTAuditors,
                    RTSmark = requesttransfers.RTSmark,
                    AllocationProductAppId=allocationproductapp.AllocationProductAppId,
                    ProductId = allocationproductapp.ProductId,
                    PNumber = allocationproductapp.PNumber,
                    PName = allocationproductapp.PName,
                    PSpec = allocationproductapp.PSpec,
                    PUnit = allocationproductapp.PUnit,
                    APANumberApplication = allocationproductapp.APANumberApplication,
                    APASmark = allocationproductapp.APASmark,
                });
            if (!string.IsNullOrEmpty(request.RTName))
            {
                list = list.Where(requesttransfers => requesttransfers.RTName.Contains(request.RTName));
            }
            if (!string.IsNullOrEmpty(request.RTNumber))
            {
                list = list.Where(requesttransfers => requesttransfers.RTNumber.Contains(request.RTNumber));
            }
            var totalCount = await list.CountAsync();

            // 应用分页  
            var preproductionlist = await list
                .OrderByDescending(requesttransfers => requesttransfers.RequestTransfersId)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToListAsync();

            // 准备结果  
            var pages = new PageDTO<SelectRequestTransfersDto>
            {
                TotalCount = totalCount,
                PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize),
                list = preproductionlist
            };
            return pages;
        }
    }
}
